//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.lucene.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

import com.farm.lucene.constant.IndexConf;

public class IqlAnalyse implements IqlAnalyseInter {
    private static final Logger log = Logger.getLogger(IqlAnalyse.class);
    private String iql;
    private String title;
    private String value;
    private String group;
    private int currentPage;
    private int pageSize;
    private static int LUCENE_QUERY_MAXNUM;

    public IqlAnalyse(String IQL, int cPage, int psize) {
        String iqlpart;
        if (LUCENE_QUERY_MAXNUM == 0) {
            iqlpart = null;

            try {
                iqlpart = IndexConf.getString("config.file.luncene_max_query_num");
                LUCENE_QUERY_MAXNUM = Integer.valueOf(iqlpart).intValue();
            } catch (Exception var6) {
                log.error("系统参数LUCENE_QUERY_MAXNUM错误:" + iqlpart);
            }
        }

        this.iql = IQL.trim();
        if (this.iql.indexOf("WHERE(") >= 0) {
            iqlpart = this.iql.substring(this.iql.indexOf("WHERE(") + "WHERE(".length(), this.iql.indexOf(")"));
            String[] whereArray = iqlpart.split("=");
            if (whereArray.length < 2) {
                throw new RuntimeException("where错误");
            }

            this.title = whereArray[0];
            this.value = whereArray[1];
        }

        if (this.iql.indexOf("ORDER_BY(") >= 0) {
            iqlpart = this.iql.substring(this.iql.indexOf("ORDER_BY("));
            this.group = iqlpart.substring(iqlpart.indexOf("ORDER_BY(") + "ORDER_BY(".length(), iqlpart.indexOf(")"));
        }

        this.currentPage = cPage;
        this.pageSize = psize;
    }

    public static void main(String[] args) {
    }

    public String[] getLimitTitle() {
        return this.title.trim().toUpperCase().split(",");
    }

    public String getLimitValue() {
        return QueryParser.escape(this.value.replaceAll("\\(", "").replaceAll("\\)", ""));
    }

    public int getMaxTopNum() {
        return LUCENE_QUERY_MAXNUM;
    }

    public Sort getSortTitle() {
        Sort sort = new Sort();
        if (this.group != null && this.group.trim().length() > 0) {
            String[] sortstr = this.group.split(",");
            SortField[] fields = new SortField[sortstr.length];

            try {
                for(int i = 0; i < sortstr.length; ++i) {
                    String sortString = sortstr[i];
                    String[] para = sortString.trim().split(" ");
                    String[] para2 = para[0].trim().split(":");
                    String title = para2[0].trim().toUpperCase();
                    String type = para2[1].trim().toUpperCase();
                    String sortType = para[1].trim().toUpperCase();
                    int typeInt = 0;
                    if (title == null || title.length() <= 0 || type == null || type.length() <= 0 || sortType == null || sortType.length() <= 0) {
                        throw new RuntimeException("排序语句错误" + this.group);
                    }

                    if (type.equals("DOUBLE")) {
                        typeInt = 7;
                    }

                    if (type.equals("STRING")) {
                        typeInt = 3;
                    }

                    if (type.equals("LONG")) {
                        typeInt = 6;
                    }

                    fields[i] = new SortField(title, typeInt, sortType.toUpperCase().equals("DESC"));
                }

                sort.setSort(fields);
                return sort;
            } catch (Exception var12) {
                log.error("排序条件解析错误" + var12);
                throw new RuntimeException("排序语句错误" + this.group + "/" + var12);
            }
        } else {
            return sort;
        }
    }

    public ScoreDoc[] subDoc(ScoreDoc[] allDoc) {
        int curentSize = allDoc.length - (this.currentPage - 1) * this.pageSize;
        if (curentSize <= 0) {
            curentSize = 0;
        }

        if (curentSize > this.pageSize) {
            curentSize = this.pageSize;
        }

        ScoreDoc[] newScore = new ScoreDoc[curentSize];
        int m = 0;

        for(int i = (this.currentPage - 1) * this.pageSize; i < (this.currentPage - 1) * this.pageSize + curentSize && allDoc.length >= i; ++i) {
            newScore[m] = allDoc[i];
            ++m;
        }

        return newScore;
    }

    public List<ScoreDoc> subDoc(List<ScoreDoc> allDoc) {
        int curentSize = allDoc.size() - (this.currentPage - 1) * this.pageSize;
        if (curentSize <= 0) {
            curentSize = 0;
        }

        if (curentSize > this.pageSize) {
            curentSize = this.pageSize;
        }

        List<ScoreDoc> newScore = new ArrayList();

        for(int i = (this.currentPage - 1) * this.pageSize; i < (this.currentPage - 1) * this.pageSize + curentSize && allDoc.size() >= i; ++i) {
            newScore.add((ScoreDoc)allDoc.get(i));
        }

        return newScore;
    }

    public String getIQL() {
        return this.iql;
    }
}
